import 'package:crm/utils/theme_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';

class BusinessPage extends StatefulWidget {
  BusinessPage({Key key}) : super(key: key);

  @override
  _BusinessPageState createState() => _BusinessPageState();
}

class _BusinessPageState extends State<BusinessPage>
    with WidgetsBindingObserver {
  TextEditingController _controller = TextEditingController();
  FocusNode focusNode = FocusNode();
  String _inputText = "";
  bool _hasdeleteIcon = false;
  bool isKeyboardActived = false;
  //创建一个界面变化的观察者
  @override
  void dispose() {
    _controller?.dispose();
    focusNode.dispose();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeMetrics() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      // 当前是安卓系统并且在焦点聚焦的情况下
      if (focusNode.hasFocus) {
        if (isKeyboardActived) {
          isKeyboardActived = false;
          // 使输入框失去焦点
          focusNode.unfocus();
          return;
        }
        isKeyboardActived = true;
      }
    });
    super.didChangeMetrics();
  }

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void didUpdateWidget(BusinessPage oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商机'),
      ),
      body: GestureDetector(
        onTap: () => FocusScope.of(context).unfocus(),
        child: Stack(
          children: <Widget>[
            Positioned(
              top: 0,
              left: 0,
              width: ScreenUtil().setWidth(750),
              child: Container(
                child: TextField(
                  controller: _controller,
                  focusNode: focusNode,
                  onEditingComplete: () {
                    print('失去焦点');
                    focusNode.unfocus();
                  },
                  onChanged: (val) {
                    setState(
                      () {
                        _inputText = val;
                        _hasdeleteIcon = val.isNotEmpty;
                      },
                    );
                  },
                  decoration: InputDecoration(
                    hintText: '请输入搜索内容',
                    suffixIcon: _hasdeleteIcon
                        ? IconButton(
                            alignment: Alignment.center,
                            padding: const EdgeInsets.all(0.0),
                            iconSize: 18.0,
                            icon: Icon(Icons.clear),
                            onPressed: () {
                              setState(() {
                                _inputText = '';
                                _controller.text = '';
                                _hasdeleteIcon = _inputText.isNotEmpty;
                              });
                            })
                        : Text(''),
                    prefixIcon: Icon(Icons.search),
                  ),
                ),
              ),
            ),
            _left()
          ],
        ),
      ),
    );
  }

  Widget _left() {
    return Container(
        margin: EdgeInsets.only(top: ScreenUtil().setHeight(100)),
        child: ListView.builder(
          itemCount: 10,
          itemBuilder: (BuildContext context, int index) {
            return _leftList();
          },
        ));
  }

  Widget _leftList() {
    return Container(
      margin: EdgeInsets.all(10.0),
      padding: EdgeInsets.only(right: 10.0),
      decoration: BoxDecoration(
        color: ThemeUtils.getDialogBackgroundColor(context),
        boxShadow: [
          BoxShadow(
              color: Colors.black12,
              offset: Offset(0.0, 15.0), //阴影xy轴偏移量
              blurRadius: 15.0, //阴影模糊程度
              spreadRadius: 1.0 //阴影扩散程度
              )
        ],
        borderRadius: BorderRadius.all(
          Radius.circular(10.0),
        ),
      ),
      height: ScreenUtil().setHeight(200),
      child: InkWell(
        onTap: () {
          print('object');
          focusNode.unfocus();
        },
        child: Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: Center(
                child: Icon(Icons.assignment),
              ),
            ),
            Expanded(
              flex: 5,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('安徽柠徽信息工程有限公司'),
                  Text('创建人:张远涛'),
                  FittedBox(
                      child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text('创建:2020-06-04'),
                      Text('最后跟进:2020-06-04'),
                    ],
                  ))
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
